﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;
using DbHelp;

namespace IotherRis
{
    public static class Register
    {
        public static string KeyCPU()
        {
            string str2;
            try
            {
                string str3 = "";
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT   *   FROM   Win32_Processor");
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    str3 = Convert.ToString(RuntimeHelpers.GetObjectValue(RuntimeHelpers.GetObjectValue(RuntimeHelpers.GetObjectValue(obj2["ProcessorId"]))));
                }
                str2 = str3.ToString();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                str2 = "";
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        public static string f_register_SS(string pKeys)
        {
            string expression = "";
            string str7 = pKeys.Trim();
            if (str7 != null)
            {
                expression = expression + str7;
            }
            string str4 = "";
            string str5 = "";
            int num5 = Strings.Len(expression) - 1;
            str4 = str7;
            for (int i = 0; i <= num5; i++)
            {
                if (i == 0)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 10));
                }
                else if (i == 1)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 6));
                }
                else if (i == 2)
                {
                    str5 = str5 + "S";
                }
                else if (i == 3)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 11));
                }
                else if (i == 4)
                {
                    str5 = str5 + "S";
                }
                else if (i == 5)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 8));
                }
                else if (i == 6)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 8));
                }
                else if (i == 7)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 6));
                }
                else if (i == 8)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 9));
                }
                else if (i == 9)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 7));
                }
                else if (i == 10)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 3));
                }
                else if (i == 11)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 5));
                }
                else if (i == 12)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 8));
                }
                else if (i == 13)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 6));
                }
                else if (i == 14)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 4));
                }
                else if (i == 15)
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) - 3));
                }
                else
                {
                    str5 = str5 + Conversions.ToString(Strings.Chr(Strings.Asc(str4.Substring(i, 1)) + 5));
                }
            }
            string str = "";
            long num2 = Strings.Len(Strings.Trim(str5));
            long num7 = num2;
            for (long j = 1L; j <= num7; j += 1L)
            {
                string input = Strings.Mid(Strings.Trim(str5), (int)j, 1);
                if (Regex.IsMatch(input, "[a-z0-9A-Z]"))
                {
                    str = str + input;
                }
            }

            str5 = str;
            return str5;
        }

        public static bool f_isRegister()
        {
            bool mReturn = false;


            string mCode = AccessHelper.IniReadValue("SERVER", "CODE", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");

            byte[] mByteCode = Convert.FromBase64String(mCode);
            string mKeys = System.Text.Encoding.UTF8.GetString(mByteCode);

            if (f_register_SS(KeyCPU()) == mKeys)
            {
                mReturn = true;
            }
            else
            {
                mReturn = false;
            }
            return mReturn;

        }

        public static void f_cleaRegister()
        {
            AccessHelper.IniWriteValue("SERVER", "CODE", "", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");
        }
    }
}
